package com.qiaodata.datatrans.utils;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.*;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.types.ObjectId;

public class MongoDBUtil {

//	 private static final String HOST = "192.168.6.97";//正式
	private static final String HOST = "127.0.0.1";// 本地测试

//	 private static final Integer port =40000;//正式
	private static final Integer port = 27017;// 测试

//	public static MongoClient mongoClient = new MongoClient(HOST, port);// 建立连接

	public static DBObject getDBObjectById(String id, String dbName, String tableName) {
		MongoClient mongoClient = new MongoClient(HOST, port);// 建立连接
		try {
			DB db = mongoClient.getDB(dbName);// 数据库名
			BasicDBObject filter_dbobject = new BasicDBObject();
			filter_dbobject.put("_id", new ObjectId(id));
			DBCollection collection = db.getCollection(tableName);// 集合名，对应mysql中的表名
			DBObject dbObject = collection.findOne(filter_dbobject);
			if (null != mongoClient) {
				try {
					mongoClient.close();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				mongoClient = null;
			}
			return dbObject;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != mongoClient) {
				try {
					mongoClient.close();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				mongoClient = null;
			}
		}
		return null;
	}

	public static void insertDocument(Document document, String dbName, String tableName) {
		MongoClient mongoClient = new MongoClient(HOST, port);// 建立连接
		try {
			MongoDatabase database = mongoClient.getDatabase(dbName);// 数据库名
			MongoCollection<Document> collection = database.getCollection(tableName);// 集合名，对应mysql中的表名
			collection.insertOne(document);
			mongoClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != mongoClient) {
				try {
					mongoClient.close();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				mongoClient = null;
			}
		}
	}

	public static Document getDocumentById(String id, String dbName, String tableName) {
		MongoClient mongoClient = new MongoClient(HOST, port);// 建立连接
		try {
			MongoDatabase database = mongoClient.getDatabase(dbName);// 数据库名
			MongoCollection<Document> collection = database.getCollection(tableName);// 集合名，对应mysql中的表名
			Document document = new Document();
			document.put("_id", new ObjectId(id));//集合id，唯一
			FindIterable<Document> findIterable = collection.find(document);
			MongoCursor<Document> cursor = findIterable.iterator();
			Document tryNext = null;
			while (cursor.hasNext()) {
				tryNext = cursor.tryNext();
			}
			mongoClient.close();
			
			return tryNext;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != mongoClient) {
				try {
					mongoClient.close();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				mongoClient = null;
			}
		}
		
		return null;
	}

	public static void main(String[] args) {
		DBObject dbObjectById1 = MongoDBUtil.getDBObjectById("558f465e1693d8fa130045ff", "test", "quanzhi1");
		DBObject dbObjectById2 = MongoDBUtil.getDBObjectById("558f465e1693d8fa13004601", "test", "quanzhi2");
		System.out.println(dbObjectById1);
		System.out.println(dbObjectById2);
		JSONObject parse1 = (JSONObject) JSONObject.parse(dbObjectById1.toString());
		System.out.println(parse1);
		JSONObject parse2 = (JSONObject) JSONObject.parse(dbObjectById2.toString());
		System.out.println(parse2);
	}
	
}
